package com.yonglin.mvvmapplication.view

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.yonglin.mvvmapplication.R
import com.yonglin.mvvmapplication.model.AccountBean
import com.yonglin.mvvmapplication.viewmodel.AccountViewModel
import kotlinx.android.synthetic.main.fragment_one.*

class FragmentOne : Fragment() {
    private lateinit var mModel: AccountViewModel
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_one, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        mModel=ViewModelProviders.of(this.activity!!).get(AccountViewModel::class.java)
        fragment_one_btn.setOnClickListener {
            mModel.getAccount().postValue(AccountBean("fragment_one","12345678910"))
        }
        mModel.getAccount().observe(this, Observer {
            fragment_one_tv.text=it.name+": "+it.telPhone
        })
    }

}